package o2o.web.actions.order;

import com.jinyou.utils.common.JYListSort;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.agent.ShopAgentRelationMemory;
import o2o.cache.agent.UserAgentRelationMemory;
import o2o.cache.order.OrderGoodsLabelMemory;
import o2o.cache.order.OrderGoodsMemory;
import o2o.cache.shop.ShopMemory;
import o2o.dao.PO.agent.UserAgentRelation;
import o2o.dao.PO.order.OrderGoods;
import o2o.dao.PO.order.OrderGoodsLabel;
import o2o.dao.PO.order.OrderInfo;
import o2o.company.model.ShopInfo;
import o2o.dao.VO.order.OrderGoodsLabelVO;
import o2o.dao.VO.order.OrderGoodsVO;
import o2o.dao.VO.order.OrderInfoVO;
import o2o.system.data.SYS_CONFIG_PATH;

import java.util.ArrayList;
import java.util.List;

//订单列表辅助类
public class OrderListMan {

    //转化为vo  list
    public static List<OrderInfoVO> convertToVo(List<OrderInfo> list, Boolean needGoods) {
        if (ValidateUtil.isAbsList(list)) {
            List<OrderInfoVO> voList = new ArrayList<OrderInfoVO>();
            for (int i = 0; i < list.size(); i++) {
                if (null == list.get(i)) {
                    continue;
                }

                OrderInfoVO orderInfoVO = new OrderInfoVO();
                try {
                    ObjectUtils.copyPropertiesExclude(list.get(i), orderInfoVO, null);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
                if (null == orderInfoVO) {
                    continue;
                }
                if (needGoods) {
                    if (null == orderInfoVO.getGoods() && ValidateUtil.isNotNull(orderInfoVO.getOrderNo())) {
                        List<OrderGoods> orderGoods = OrderGoodsMemory.getInstance().listOfOrderNo(orderInfoVO.getOrderNo());
                        if (ValidateUtil.isAbsList(orderGoods)) {
                            List<OrderGoodsVO> goodsVOList = new ArrayList<OrderGoodsVO>();
                            for (int j = 0; j < orderGoods.size(); j++) {
                                if (null == orderGoods.get(j)) {
                                    continue;
                                }
                                OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
                                try {
                                    ObjectUtils.copyPropertiesExclude(orderGoods.get(j), orderGoodsVO, null);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    continue;
                                }
                                if (null == orderGoodsVO) {
                                    continue;
                                }
                                goodsVOList.add(orderGoodsVO);
                            }
                            if (ValidateUtil.isAbsList(goodsVOList)) {
                                orderInfoVO.setGoods(goodsVOList);
                            }
                        }
                    }
                }


                if (ValidateUtil.isNotNull(orderInfoVO.getShopImageUrl()) && ValidateUtil.isNotNetUrl(orderInfoVO.getShopImageUrl())) {
//                    orderInfoVO.setShopImageUrl(Common.imageUrl + orderInfoVO.getShopImageUrl());
                    orderInfoVO.setShopImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + orderInfoVO.getShopImageUrl());
                }
                if (ValidateUtil.isNotNull(orderInfoVO.getPostmanImageUrl()) && ValidateUtil.isNotNetUrl(orderInfoVO.getPostmanImageUrl())) {
//                    orderInfoVO.setPostmanImageUrl(Common.imageUrl + orderInfoVO.getPostmanImageUrl());
                    orderInfoVO.setPostmanImageUrl(SYS_CONFIG_PATH.IMAGE_BASE_URL + orderInfoVO.getPostmanImageUrl());
                }

                voList.add(orderInfoVO);
            }
            if (ValidateUtil.isAbsList(voList)) {
                return voList;
            }
        }
        return null;
    }

    //获取标签
    public static List<OrderGoodsLabelVO> listOfOrderNO(String orderNo) {
        if (ValidateUtil.isNull(orderNo)) {
            return null;
        }
        List<OrderGoodsLabel> list = OrderGoodsLabelMemory.getInstance().listOfOrderNo(orderNo);
        if (!ValidateUtil.isAbsList(list)) {
            return null;
        }
        List<OrderGoodsLabelVO> voList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (null == list.get(i)) {
                continue;
            }

            OrderGoodsLabelVO vo = new OrderGoodsLabelVO();
            try {
                ObjectUtils.copyPropertiesExclude(list.get(i), vo, null);
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            voList.add(vo);
        }

        return voList;
    }

    //排序
    public static List<OrderInfo> sort(List<OrderInfo> list) {
        JYListSort.sortDesc(list, "createTime");
        return list;
    }

    public static List<OrderInfoVO> sortVO(List<OrderInfoVO> list) {
        JYListSort.sortDesc(list, "createTime");
        return list;
    }


    // 根据某城市代理下的商圈下的订单
    private static Boolean isCityAgent(String username, Long shopId) {
        if (ValidateUtil.isNull(username) || ValidateUtil.isNotID(shopId)) {
            return false;
        }

        //获取某城代账号下所有商圈
        List<UserAgentRelation> uAList = UserAgentRelationMemory.getInstance().listOfUsername(username);
        if (ValidateUtil.isAbsList(uAList)) {
            for (int x = 0; x < uAList.size(); x++) {
                if (ValidateUtil.isNotID(uAList.get(x).getAgentId())) {
                    return false;
                }
                // 根据代理获取店铺ID列表
                List<Long> saList = ShopAgentRelationMemory.getInstance().listOfAgentId(uAList.get(x).getAgentId());
                if (ValidateUtil.isAbsList(saList)) {
                    for (int y = 0; y < saList.size(); y++) {
                        if (null == saList.get(y)) {
                            return false;
                        }
                        // 根据Id获取该店铺信息
                        ShopInfo shopInfo = ShopMemory.getInstance().getShopById(saList.get(y));
                        if (null != shopInfo && shopInfo.getId() - shopId != 0) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    // 根据城市代理获取所有店铺Id
    public static List<Long> getShopIdList(String username) {
        //获取某城代账号下所有商圈
        List<Long> shopIdList = null;
        List<UserAgentRelation> uAList = UserAgentRelationMemory.getInstance().listOfUsername(username);
        if (ValidateUtil.isAbsList(uAList)) {
            // 所有代理下的店铺Id
            shopIdList = new ArrayList<>();
            for (int x = 0; x < uAList.size(); x++) {
                if (ValidateUtil.isNotID(uAList.get(x).getAgentId())) {
                    continue;
                }
                // 根据代理获取店铺ID列表
                List<Long> IdList = ShopAgentRelationMemory.getInstance().listOfAgentId(uAList.get(x).getAgentId());
                if (ValidateUtil.isAbsList(IdList)) {
                    for (int i = 0; i < IdList.size(); i++) {
                        if (null == IdList.get(i)) {
                            continue;
                        }
                        shopIdList.add(IdList.get(i));
                    }
                }

            }
            return shopIdList;
        }
        return null;
    }
}
